<template>
  <scroll-view
    class="content"
    scroll-y
    :style="{ height: widHeigh + 'px' }"
    lower-threshold="60"
    @scrolltolower="onreachBottom"
  >
    <view class="search">
      <u-search
        placeholder="请输入"
        bg-color="#292B2B"
        placeholder-color="#999999"
        @change="handlerSearch"
        @search="handlerSearch"
        v-model="keyWord"
        :clearabled="false"
        :show-action="false"
      ></u-search>
    </view>

    <view class="exhibit" v-if="productList.length != 0">
        <view
          class="exhibit_item"
          v-for="(item, index) in productList"
          :key="item.id"
          @click="toGoodsDetail(item)"
        >
          <view class="exhibit_item_wrapper">
            <view class="exhibit_item_timer">
              <image
                class="exhibit_item_timerIcon"
                src="../../static/index_timer.png"
              ></image>

              <view class="exhibit_item_timerText">
                <view
                  v-if="
                    is_Coming_soon(item.opening_at) &&
                    !productList[index].startstate
                  "
                >
                  <text class="exhibit_item_sale1">即将开售</text>
                  <text class="exhibit_item_sale2">{{ item.start_at }}</text>
                </view>

                <view v-else-if="productList[index].startstate">
                  <text class="exhibit_item_sale1">即将开售</text>
                  <u-count-down
                    :timestamp="item.countDown"
                    separator="colon"
                    separator-color="#F78623"
                    style="margin-left: 8rpx"
                    separator-size="28"
                    font-size="28"
                    @end="end(index)"
                    color="#F78623"
                    :show-border="false"
                    bg-color="##000000"
                  ></u-count-down>
                  <!-- <view class="unpaid_left">后关闭</view> -->

                  <!-- <text class="exhibit_item_sale1">开售</text> -->
                </view>

                <view v-else>
                  <text class="exhibit_item_sale2">已开售</text>
                </view>
              </view>
            </view>

            <view
              class="exhibit_item_timer"
              style="margin-top: 40px"
              v-if="
                item.first_buy_at != null &&
                userInfo &&
                userInfo.total_first_buy_num != 0 &&
                productList[index].firstcountDown > 0
              "
            >
              <image
                class="exhibit_item_timerIcon"
                src="../../static/index_timer.png"
              ></image>

              <view class="exhibit_item_timerText">
                <view
                  v-if="
                    !productList[index].firststate &&
                    productList[index].firstcountDown > 0
                  "
                >
                  <text class="exhibit_item_sale1">优先购时间</text>
                  <text class="exhibit_item_sale2">{{
                    item.first_start_at
                  }}</text>
                </view>

                <view
                  v-else-if="
                    productList[index].firststate &&
                    productList[index].firstcountDown > 0
                  "
                >
                  <text class="exhibit_item_sale1">优先购时间</text>
                  <u-count-down
                    :timestamp="item.firstcountDown"
                    separator="colon"
                    separator-color="#F78623"
                    style="margin-left: 8rpx"
                    separator-size="28"
                    font-size="28"
                    @end="end(index)"
                    color="#F78623"
                    :show-border="false"
                    bg-color="##000000"
                  ></u-count-down>
                </view>
              </view>
            </view>

            <image class="exhibit_item_goodsImage" :src="item.cover"></image>
          </view>

          <view class="exhibit_item_bottom">
            <view class="exhibit_item_row">
              <view class="exhibit_item_column1">{{ item.name }}</view>
              <view class="exhibit_item_column2">
                <view class="exhibit_item_limited">限量</view>
                <view class="exhibit_item_inStock">{{ item.total_num }}份</view>
              </view>
            </view>
            <view class="exhibit_item_row">
              <view class="exhibit_item_column3">
                <image
                  class="exhibit_item_avatar"
                  :src="item.creator_avatar"
                ></image>
                <view class="exhibit_item_username">{{
                  item.creator_name
                }}</view>
              </view>
              <view class="exhibit_item_column4">¥ {{ item.price }}</view>
            </view>

            <view class="exhibit_item_row">
              <view class="exhibit_item_newButton">
                <template v-if="item.type == 1">
                  <text v-if="item.is_have_surplus_num == 0">已售罄</text>

                  <text
                    v-else-if="
                      userInfo &&
                      userInfo.is_frist_buy == 1 &&
                      item.first_buy_at &&
                      !is_Coming_soon(item.first_buy_at)
                    "
                    >抢购</text
                  >

                  <text v-else>抢购</text>
                </template>

                <template v-else>
                  <text v-if="!is_Coming_soon(item.opening_at)">已结束</text>

                  <text
                    v-else-if="
                      item.is_reserved_apply == 1 &&
                      is_Coming_soon(item.opening_at)
                    "
                    >已预约</text
                  >

                  <text v-else>预约</text>
                </template>
              </view>
            </view>

            <!-- <view class="exhibit_item_left">
              <view class="exhibit_item_first">{{ item.name }}</view>
              <view class="exhibit_item_group">
                <view class="exhibit_item_second">
                  <view class="exhibit_item_limited">限量</view>
                  <view class="exhibit_item_inStock"
                    >{{ item.total_num }}份</view
                  >
                </view>
                <view
                  class="exhibit_item_second"
                  v-if="item.is_show_surplus_num"
                >
                  <view class="exhibit_item_limited">剩余</view>
                  <view class="exhibit_item_inStock"
                    >{{ item.surplus_num }}份</view
                  >
                </view>
              </view>
              <view class="exhibit_item_third">
                <image
                  class="exhibit_item_avatar"
                  :src="item.creator_avatar"
                ></image>
                <view class="exhibit_item_username">{{
                  item.creator_name
                }}</view>
              </view>
            </view> -->

            <!-- 	<view class="" style="color: #FFFFFF;">
					<text>{{item.no}}</text>
				</view> -->

            <!-- <view class="exhibit_item_right">
              <template v-if="item.type == 1">
                <view
                  v-if="item.is_have_surplus_num == 0"
                  class="exhibit_item_buttonGray"
                  >已售罄</view
                >

                <view
                  v-else-if="
                    userInfo &&
                    userInfo.is_frist_buy == 1 &&
                    item.first_buy_at &&
                    !is_Coming_soon(item.first_buy_at)
                  "
                  class="exhibit_item_button"
                  >抢购</view
                >
                <view
                  v-else
                  class="exhibit_item_button"
                  :class="
                    is_Coming_soon(item.opening_at)
                      ? 'exhibit_item_buttonGray'
                      : ''
                  "
                  >抢购</view
                >
              </template>
              <template v-else>
                <view
                  v-if="!is_Coming_soon(item.opening_at)"
                  class="exhibit_item_buttonGray"
                  >已结束</view
                >

                <view
                  v-else-if="
                    item.is_reserved_apply == 1 &&
                    is_Coming_soon(item.opening_at)
                  "
                  class="exhibit_item_buttonGray"
                  >已预约</view
                >

                <view v-else class="exhibit_item_button">预约</view>
              </template>

              <view class="exhibit_item_price">¥ {{ item.price }}</view>
            </view> -->
          </view>
        </view>
      </view>

    <empty :show.sync="is_empty" :search_empty="true"></empty>
  </scroll-view>
</template>

<script>
import api from "@/api/api.js";
import tip from "@/utils/tip.js";
import moment from "moment";
import { isLogin } from "@/utils/auth.js";
import empty from "@/components/empty/empty";
export default {
  components: {
    empty,
  },
  data() {
    return {
      widHeigh: 0,
      nav_list: [
        { name: "热门产品" },
        { name: "盲盒专区" },
        { name: "产品分类" },
      ],
      current: 0,
      barStyle: {
        background: "transparent",
      },
      swiperIndex: "",

      page: 1,
      limit: 10,
      productList: [],
      keyWord: "",
      order_no: "",
      is_empty: false,

      userInfo: null,
    };
  },
  onLoad() {
    let windowElement = uni.getSystemInfoSync();
    this.widHeigh = windowElement.windowHeight;
  },
  onShow() {
    // this.getProductList(true, '');
    this.getUserInfo();
  },
  computed: {
    is_Coming_soon() {
      return (time) => {
        return moment(time).diff(moment(), "second") > 0;
      };
    },
  },
  methods: {
    // Swiper改变时获取到下标
    swiperChange(index) {
      this.swiperIndex = index;
    },
    //双击事件
    maskTouchend() {},

    async getUserInfo() {
      let res = await api.info({
        method: "get",
        query: {},
      });
      if (res.code === 0) {
        this.userInfo = res.data;
      } else {
        tip.defaults(res.msg);
      }
    },

    async getProductList(refresh, value) {
      if (refresh) {
        this.page = 1;
      }
      let res = await api.productList({
        method: "get",
        query: {
          page: this.page,
          limit: this.limit,
          keyword: value ? value : "",
        },
      });
      if (res.code === 0) {
        if (refresh) {
          this.productList = res.data;
        } else {
          this.productList = this.productList.concat(res.data);
        }

        if (this.productList.length == 0) {
          this.is_empty = true;
        } else {
          this.is_empty = false;
        }
      } else {
        tip.defaults(res.msg);
      }
    },

    async handleCreateOrder(product_id, time) {
      // if (moment(time).diff(moment(), "second") > 0) {
      //   tip.defaults("未到开售时间");
      //   return;
      // }
      // const res = await api.productCreateOrder({
      //   method: "post",
      //   query: {
      //     product_id: product_id,
      //   },
      // });
      // if (res.code == 0) {
      //   this.order_no = res.data;
      //   if (res.data) {
      //     this.pay();
      //   }
      // } else {
      //   tip.defaults(res.msg);
      // }
    },

    async pay() {
      const res = await api.pay({
        method: "post",
        query: {
          pay_type: 3,
          order_no: this.order_no,
        },
      });

      if (res.code == 0) {
        var params = res.data;
        // App上生效
        let orderInfo = {
          appid: params.appid, //对应的appid
          noncestr: params.noncestr,
          package: params.package,
          partnerid: params.partnerid,
          prepayid: params.prepayid,
          timestamp: params.timestamp,
          sign: params.sign, //二次签名
        };
        uni.requestPayment({
          provider: "wxpay", // wxpay、alipay
          orderInfo: JSON.stringify(orderInfo), //微信、支付宝订单数据
          success: function (res) {
            tip.success("支付成功");
            console.log("res", res);
          },
          fail: function (err) {
            tip.defaults("支付失败");
            console.log("err", err);
          },
        });
      } else {
        tip.defaults(res.msg);
      }
    },

    // 预约
    async handleReservedApply(product_id) {
      // const res = await api.reservedApply({
      //   method: "post",
      //   query: {
      //     product_id: product_id,
      //   },
      // });
      // if (res.code == 0) {
      //   tip.defaults("预约成功");
      // } else {
      //   tip.defaults(res.msg);
      // }
    },

    handlerSearch(value) {
      this.$u.debounce(this.getProductList(true, value), 1000, false);
    },

    async toGoodsDetail(item) {
      await isLogin();
      uni.navigateTo({
        url: `../../pages/goods_detail/goods_detail?product_id=${item.id}`,
      });
    },
    onreachBottom() {
      console.log("触发 onreachBottom");
      this.page++;
      this.$u.throttle(this.getProductList(false, ""), 1000, true);
    },
  },
};
</script>

<style lang="scss" scoped>
@import "../../style/yuanchuang.scss";
.content {
  font-family: PingFangSC-Regular, PingFang SC;
  padding: 0 30rpx 0 30rpx;
  background: $bg-color1;

  // background-image: url(../../static/index_bg.png);
  // background-repeat: no-repeat;
  // background-size: 100% 510rpx;

  // background-image: url("/static/background_mine.png");
  // background-repeat: no-repeat;
  // background-size: 100%;
}

.search {
  width: 690rpx;
  height: 88rpx;
  display: flex;
  align-items: center;
  justify-content: space-between;
  &_logo {
    width: 192rpx;
    height: 54rpx;
  }
  &_icon {
    width: 40rpx;
    height: 40rpx;
  }
}
.top {
  width: 690rpx;
  height: 280rpx;
  background: black;
  border-radius: 24rpx;
  &_image1 {
    width: 690rpx;
    height: 280rpx;
  }
}
.u-tabs_line {
  display: flex;
  position: relative;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  .u-tabs_wrapper {
    // width: 70%;
    width: 25%;
    position: relative;
    z-index: 1;
    .gradient {
      width: 100%;
      height: 100rpx;
      position: absolute;
      z-index: 0;
      top: 0;
      left: 0;
      display: flex;
      align-items: center;
      justify-content: space-around;
      &_item {
        // flex: 1;
        width: 120rpx;
        height: 20rpx;
      }
      &_active {
        filter: blur(8rpx);
        // background: linear-gradient(180deg, #39a2ff 0%, #d5280a 100%);
      }
    }
  }
}
.exhibit {
  min-height: calc(100vh - 392rpx);
  overflow: hidden;
  &_item {
    width: 690rpx;
    // height: 904rpx;
    height: 992rpx;
    background: #1d1d28;
    border-radius: 36rpx;

    background-image: url("/static/index_goods_bg.png");
    background-repeat: no-repeat;
    background-position: center;
    background-size: cover;
    -webkit-background-size: cover;
    -moz-background-size: cover;
    -o-background-size: cover;
    &_wrapper {
      position: relative;
      width: 690rpx;
      height: 690rpx;
    }
    &_timer {
      z-index: 1;
      position: absolute;
      top: 26rpx;
      left: 26rpx;

      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 0 20rpx;
      // width: 408rpx;
      height: 60rpx;
      border-radius: 30rpx;
      // opacity: 0.46;
      // background-color: rgba(0, 0, 0, 0.46);

      background: #000000;
      opacity: 0.5;
    }
    &_timerIcon {
      width: 32rpx;
      height: 32rpx;
    }
    &_timerText {
      font-size: 28rpx;
      font-weight: 400;
      line-height: 40rpx;
      // opacity: 0.46;
    }
    &_sale1 {
      color: #ffffff;
      margin-left: 8rpx;
    }
    &_sale2 {
      color: #ffffff;
      margin-left: 8rpx;
    }
    &_goodsImage {
      border-radius: 36rpx;
      width: 690rpx;
      height: 690rpx;
    }
    &_bottom {
      margin-top: 30rpx;
      // display: flex;
      // align-items: flex-end;
      // justify-content: space-between;
    }
    &_row {
      padding: 0 30rpx;
      display: flex;
      align-items: center;
      justify-content: space-between;
    }
    &_row:not(:first-child) {
      margin-top: 16rpx;
    }
    &_column1 {
      width: 400rpx;
      height: 44rpx;
      font-size: 32rpx;
      font-weight: 500;
      color: #ffffff;
      line-height: 44rpx;

      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
    &_column2 {
      display: flex;
      align-items: center;

      margin: 20rpx 0;
      font-size: 24rpx;
      font-weight: 400;
      color: #ffffff;
      line-height: 34rpx;

      // width: 202rpx;
      height: 34rpx;
      border-radius: 4rpx;
    }
    &_column3 {
      display: flex;
      align-items: center;

      font-size: 26rpx;
      font-weight: 500;
      color: #ffffff;
      line-height: 36rpx;
    }
    &_column4 {
      // width: 140rpx;
      height: 48rpx;
      font-size: 40rpx;
      font-weight: 500;
      color: #ffffff;
      line-height: 48rpx;
    }
    &_newButton {
      margin: 20rpx auto;
      width: 610rpx;
      height: 90rpx;
      background: $button;
      background-repeat: no-repeat;
      background-size: 100%;
      text-align: center;
      font-size: 32rpx;
      font-weight: 400;
      color: #ffffff;
      line-height: 90rpx;
    }
    &_left {
      margin: 30rpx 0 0 30rpx;
    }
    &_right {
      position: relative;
      height: 138rpx;
      margin: 46rpx 30rpx 0 0;
    }
    &_first {
      width: 460rpx;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;

      height: 44rpx;
      font-size: 32rpx;
      font-weight: 500;
      color: #ffffff;
      line-height: 44rpx;

      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
    &_group {
      display: flex;
    }
    &_second {
      display: flex;
      align-items: center;

      margin: 20rpx 0;
      font-size: 24rpx;
      font-weight: 400;
      color: #ffffff;
      line-height: 34rpx;

      // width: 202rpx;
      height: 34rpx;
      border-radius: 4rpx;
    }
    &_second:not(:first-child) {
      margin-left: 30rpx;
    }
    &_limited {
      // width: 76rpx;
      padding: 0 14rpx;
      height: 34rpx;
      color: $text-color4;
      background: $gradient1;
      background-repeat: no-repeat;
      background-size: 100%;
      border-radius: 4rpx;
      text-align: center;
    }
    &_inStock {
      padding: 0 14rpx;
      // margin-left: 10rpx;
      font-size: 24rpx;
      font-weight: 400;
      color: #ffffff;
      line-height: 34rpx;
      background: $bg-color2;
    }
    &_surplus {
      margin-left: 30rpx;
      width: 200rpx;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;

      height: 44rpx;
      font-size: 24rpx;
      font-weight: 500;
      color: #ffffff;
      line-height: 44rpx;
    }
    &_third {
      display: flex;
      align-items: center;

      font-size: 26rpx;
      font-weight: 500;
      color: #ffffff;
      line-height: 36rpx;
    }
    &_avatar {
      border-radius: 50%;
      margin-right: 8rpx;
      width: 36rpx;
      height: 36rpx;
    }
    &_username {
      width: 320rpx;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;

      font-size: 26rpx;
      font-weight: 500;
      color: #ffffff;
      line-height: 36rpx;
    }
    &_button {
      margin-left: calc(100% - 116rpx);
      width: 116rpx;
      height: 56rpx;
      color: $text-color4;
      background: $button;
      background-repeat: no-repeat;
      background-size: 100%;
      border-radius: 28rpx;
      text-align: center;

      font-size: 28rpx;
      font-weight: 400;
      line-height: 56rpx;
    }
    &_buttonGray {
      margin-left: calc(100% - 116rpx);
      width: 116rpx;
      height: 56rpx;
      background: #999999;
      border-radius: 28rpx;
      text-align: center;

      font-size: 28rpx;
      font-weight: 400;
      color: #ffffff;
      line-height: 56rpx;
    }
    &_price {
      position: absolute;
      right: 0;
      width: 270rpx;
      text-align: right;

      margin-top: 34rpx;
      font-size: 40rpx;
      font-family: SFPro-Bold, SFPro;
      font-weight: bold;
      color: #ffffff;
      line-height: 48rpx;
    }
  }
  &_item:not(:first-child) {
    margin-top: 30rpx;
  }
  &_item:last-child {
    margin-bottom: 30rpx;
  }
}
.popup_box {
  display: flex;
  flex-direction: column;
  align-items: center;
  // width: 50%;
  // height: 50%;

  // margin: 0 0 0 50%;
  // transform: translateX(-50%);
  padding: 30upx;
  border-radius: 16rpx !important;
}
.u-progress-content {
  display: flex;
  align-items: center;
  justify-content: center;
}

.u-progress-dot {
  // width: 16rpx;
  // height: 16rpx;
  // border-radius: 50%;
  // background-color: #fb9126;
}

.u-progress-info {
  font-size: 28rpx;
  padding-left: 16rpx;
  letter-spacing: 2rpx;
}
.popup_btn {
  margin-top: 30rpx;
  color: #ffffff;
  // background: ;
}
.popup_box_title {
  margin-bottom: 30rpx;
  text-align: center;
  color: #333;
}
.search {
  display: block;
  margin: 20rpx 0 30rpx 0;
  width: 690rpx;
}
</style>
